package com.zzw.hj48;

import java.util.Scanner;

/**
 * @Project: hw_java
 * @Description: 从单向链表中删除指定值的节点
 * @Author: zzw
 */
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int nodes = in.nextInt();
        LinkedNode head = new LinkedNode(in.nextInt());
        for (int i = 1; i < nodes; i++) {
            int data = in.nextInt();
            int pre = in.nextInt();
            LinkedNode current = head;
            while (current.data != pre) {
                current = current.next;
            }
            current.next = new LinkedNode(data, current.next);
        }
        int toRemove = in.nextInt();
        if (head.data == toRemove) {
            head = head.next;
        } else {
            LinkedNode current = head;
            while (current.next.data != toRemove) {
                current = current.next;
            }
            current.next = current.next.next;
        }
        StringBuilder sb = new StringBuilder();
        LinkedNode current = head;
        while (current != null) {
            sb.append(current.data).append(" ");
            current = current.next;
        }
        System.out.println(sb.toString().trim());
    }
}

class LinkedNode {
    int data;
    LinkedNode next;

    LinkedNode(int data) {
        this.data = data;
        this.next = null;
    }

    LinkedNode(int data, LinkedNode next) {
        this.data = data;
        this.next = next;
    }

    boolean hasNext() {
        return this.next != null;
    }

    boolean isLast() {
        return this.next == null;
    }

}